<html>
<head><meta charset="utf-8"><title>Weird error when adding #[instrument] · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html">Weird error when adding #[instrument]</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222687806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222687806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222687806">(Jan 14 2021 at 07:56)</a>:</h4>
<p>If I add <code>#[instrument(skip(self))]</code> to <code>rustc_typeck::check::method::probe::pick_by_value_method</code> I get this error:</p>
<div class="codehilite"><pre><span></span><code>Compiling rustc_typeck v0.0.0 (/home/omer/rust/rust/compiler/rustc_typeck)
error: an inner attribute is not permitted in this context
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.
</code></pre></div>
<p>I don't understand two things about this error:</p>
<ul>
<li>It doesn't have a span. Finding the problematic line was difficult..</li>
<li>I have the exact same attribute added to the method above it (<code>pick_core</code>) and it works fine.</li>
</ul>
<p>Any ideas why this one doesn't work?</p>



<a name="222687957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222687957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222687957">(Jan 14 2021 at 07:59)</a>:</h4>
<p>Wow.. I figured it out. It's the <code>//!</code> style documentation in the function that doesn't work ...</p>



<a name="222807222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222807222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222807222">(Jan 15 2021 at 01:04)</a>:</h4>
<blockquote>
<p>It doesn't have a span. Finding the problematic line was difficult..</p>
</blockquote>
<p>That's weird... <span class="user-mention" data-user-id="232545">@Joshua Nelson</span> you might want to take a look at that.</p>



<a name="222807246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222807246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222807246">(Jan 15 2021 at 01:05)</a>:</h4>
<p><code>//!</code> doc comments are parsed as "inner attributes", which is why you got that error.</p>



<a name="222809406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222809406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222809406">(Jan 15 2021 at 01:39)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D/near/222807222">said</a>:</p>
<blockquote>
<blockquote>
<p>It doesn't have a span. Finding the problematic line was difficult..</p>
</blockquote>
<p>That's weird... <span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> you might want to take a look at that.</p>
</blockquote>
<p>I would expect that to be a rogue proc macro generating dummy spans</p>



<a name="222829263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222829263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222829263">(Jan 15 2021 at 08:07)</a>:</h4>
<p>Thanks. Is it a bug (in the compiler) that this proc macro works when using <code>///</code> style documentation but not when using <code>//!</code>?</p>



<a name="222838180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/222838180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#222838180">(Jan 15 2021 at 09:56)</a>:</h4>
<p>It is not a bug in the compiler. <code>#[instrument]</code> adds code before and after the input function body and thus also the <code>//!</code> you wrote. <code>//!</code> must come at the start of an item, so rustc rejects it..</p>



<a name="223108257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223108257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223108257">(Jan 18 2021 at 11:24)</a>:</h4>
<p>I'm confused. I have this code:</p>
<div class="codehilite"><pre><span></span><code>use tracing::instrument;

#[instrument]
fn main() {
    //! blah blah
}
</code></pre></div>
<p>when I build I get the error message in my original message. <code>cargo expand</code> prints this:</p>
<div class="codehilite"><pre><span></span><code>#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use tracing::instrument;
fn main() {
    //! blah blah
    ...
</code></pre></div>
<p>This looks fine to me. It doesn't add any code before <code>//!</code>.</p>



<a name="223123797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223123797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223123797">(Jan 18 2021 at 14:10)</a>:</h4>
<p><span class="user-mention" data-user-id="119169">@osa1</span> if you feed the output of cargo expand back to rustc, do you get the same error?</p>



<a name="223124225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223124225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223124225">(Jan 18 2021 at 14:13)</a>:</h4>
<p>I get these errors when I try that:</p>
<div class="codehilite"><pre><span></span><code>error[E0433]: failed to resolve: could not find `tracing_core` in `{{root}}`
  --&gt; src/main.rs:24:27
   |
24 |                         ::tracing_core::callsite::Identifier(&amp;CALLSITE),
   |                           ^^^^^^^^^^^^ could not find `tracing_core` in `{{root}}`

error[E0433]: failed to resolve: could not find `tracing_core` in `{{root}}`
  --&gt; src/main.rs:15:43
   |
15 |                 ::tracing_core::metadata::Metadata::new(
   |                                           ^^^^^^^^ not found in `tracing_core::metadata`
   |
help: consider importing one of these items
   |
3  | use std::fs::Metadata;
   |
3  | use tracing::Metadata;
   |

error[E0433]: failed to resolve: could not find `tracing_core` in `{{root}}`
  --&gt; src/main.rs:22:44
   |
22 |                     ::tracing_core::field::FieldSet::new(
   |                                            ^^^^^^^^ not found in `tracing_core::field`
   |
help: consider importing this struct
   |
3  | use tracing::field::FieldSet;
   |
</code></pre></div>
<p>Expanded code:</p>
<div class="codehilite"><pre><span></span><code>#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use tracing::instrument;
fn main() {
    //! blah blah
    {}
    let __tracing_attr_span = {
        use ::tracing::__macro_support::{Callsite as _, Registration};
        static CALLSITE: ::tracing::__macro_support::MacroCallsite = {
            use ::tracing::__macro_support::{MacroCallsite, Registration};
            static META: ::tracing::Metadata&lt;&#39;static&gt; = {
                ::tracing_core::metadata::Metadata::new(
                    &quot;main&quot;,
                    &quot;instrument_test&quot;,
                    tracing::Level::INFO,
                    Some(&quot;src/main.rs&quot;),
                    Some(3u32),
                    Some(&quot;instrument_test&quot;),
                    ::tracing_core::field::FieldSet::new(
                        &amp;[],
                        ::tracing_core::callsite::Identifier(&amp;CALLSITE),
                    ),
                    ::tracing::metadata::Kind::SPAN,
                )
            };
            static REG: Registration = Registration::new(&amp;CALLSITE);
            MacroCallsite::new(&amp;META, &amp;REG)
        };
        let mut interest = ::tracing::collect::Interest::never();
        if tracing::Level::INFO &lt;= ::tracing::level_filters::STATIC_MAX_LEVEL
            &amp;&amp; tracing::Level::INFO &lt;= ::tracing::level_filters::LevelFilter::current()
            &amp;&amp; {
                interest = CALLSITE.interest();
                !interest.is_never()
            }
            &amp;&amp; CALLSITE.is_enabled(interest)
        {
            let meta = CALLSITE.metadata();
            ::tracing::Span::new(meta, &amp;{ meta.fields().value_set(&amp;[]) })
        } else {
            let span = CALLSITE.disabled_span();
            {};
            span
        }
    };
    let __tracing_attr_guard = __tracing_attr_span.enter();
    {}
}
</code></pre></div>



<a name="223158099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158099">(Jan 18 2021 at 19:47)</a>:</h4>
<p>I don't get any errors when I feed the output back in.</p>



<a name="223158121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158121">(Jan 18 2021 at 19:47)</a>:</h4>
<p>Maybe this <em>is</em> a compiler bug. <span class="user-mention" data-user-id="119169">@osa1</span> Can you open an issue?</p>



<a name="223158186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158186">(Jan 18 2021 at 19:48)</a>:</h4>
<p>well, I would expect --unpretty to be buggy before the compiler</p>



<a name="223158203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158203">(Jan 18 2021 at 19:48)</a>:</h4>
<p>but yeah that sounds like what I was expected, I bet the tracing proc-macro is getting confused</p>



<a name="223158211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158211">(Jan 18 2021 at 19:49)</a>:</h4>
<p>I would talk to tracing on discord</p>



<a name="223158230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158230">(Jan 18 2021 at 19:49)</a>:</h4>
<p>But the expanded output looks fine and it compiles fine...</p>



<a name="223158470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158470">(Jan 18 2021 at 19:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D/near/223158230">said</a>:</p>
<blockquote>
<p>But the expanded output looks fine and it compiles fine...</p>
</blockquote>
<p>right - which means the expanded output is not actually what the compiler sees</p>



<a name="223158487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158487">(Jan 18 2021 at 19:52)</a>:</h4>
<p>either the proc-macro or <code>unpretty</code> is buggy</p>



<a name="223158801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158801">(Jan 18 2021 at 19:57)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> yeah when I add tracing-core as a dep it compiles fine. I didn't realize before that it's using another library, I thought it's using non-public types or something (maybe those are allowed in macro-expanded code).</p>



<a name="223158805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158805">(Jan 18 2021 at 19:57)</a>:</h4>
<p>I'll open an issue</p>



<a name="223158881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158881">(Jan 18 2021 at 19:58)</a>:</h4>
<p>Agreed that it should be a bug. Code with macro doesn't compile but expanded form compiles fine.</p>



<a name="223158899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223158899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223158899">(Jan 18 2021 at 19:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119169">osa1</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D/near/223158801">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> yeah when I add tracing-core as a dep it compiles fine. I didn't realize before that it's using another library, I thought it's using non-public types or something (maybe those are allowed in macro-expanded code).</p>
</blockquote>
<p>it's using public types that have <code>doc(hidden)</code></p>



<a name="223189211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Weird%20error%20when%20adding%20%23%5Binstrument%5D/near/223189211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Weird.20error.20when.20adding.20.23.5Binstrument.5D.html#223189211">(Jan 19 2021 at 05:40)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/81183">https://github.com/rust-lang/rust/issues/81183</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>